(function($){

if (window.APP){
  return;
}

APP={
  ajax:function(api,data,callback,completeCallback){

    $.ajax({
      type: "POST",
      //timeout : 20000,
      url: api,
      // contentType: "application/json",
      // data: JSON.stringify(datas),
      data: data,
      success: function(msg){
        callback(msg);
      },
      error: function(XMLHttpRequest, textStatus, errorThrown){
        APP.alert('与服务器链接失败，请重试<br /><br />'+textStatus+'  <br />  '+errorThrown);
      },
      complete: function(v){
        if (completeCallback) completeCallback(v);
      }

    });
  },
  ajaxform:function(api,formid,callback,completeCallback){
  	APP.ajax(api, $("#"+formid).serialize(),callback,completeCallback);
  },
  goto:function(url,time){
    time = time || 1;
    time = parseInt(time);
    setTimeout(function(){
      window.location.href=url;
    },time);
    return this;
  },
  reload:function(time){
    time = time || 1;
    time = parseInt(time);
    setTimeout(function(){
      window.location.reload();
    },time);
    return this;
  },
  storage:{
    set:function(key,value){
      localStorage.setItem(key,JSON.stringify(value));
      return this;
    },
    get:function(key,defaultVal){
      var value=JSON.parse(localStorage.getItem(key));
      return value || defaultVal;
    }
  },
  confirm:function(msg,cb){
    if (!window.confirmvue){
  		window.confirmvue=new Vue();
  	}
    if (window.confirmvue.$confirm){
      window.confirmvue.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString:true,
        type: 'warning',
        // center: true,
        callback:function(res){
          if (res=="confirm"){
            if (cb) cb();
          }
        }
      });
    }else{
      if (confirm(msg)){
        if (cb) cb();
      }
    }
    return this;
  },
  alert:function(msg,types,times=3000){
    if (!window.alertvue){
  		window.alertvue=new Vue();
  	}


  	if (window.alertvue.$message){
      var type="error";
      if (types=='info'){
        type="info";
  		}else if (types){
        type="success";
  		}else{
        type="error";
  		}
      var alertSettings={
        type:type,
        dangerouslyUseHTMLString: true,
        message:msg,
        duration:times
      }
      window.alertvue.$message(alertSettings);

  	}else{
  		alert(msg);
  	}
    return this;
  },
  object: function(item){
    if (typeof(item)=="object"){
      return item;
    }else{
      return JSON.parse(item);
    }
  },
  // loadVUE: function(item){
  //   if (typeof(item)=="string"){
  //     item=[item];
  //   }
  //   for (var i in item) {
  //     var name=item[i];
  //     if (name.endsWith("/")){
  //       name=name.substring(0, name.length - 1);
  //     }
  //     id=name.substring(name.lastIndexOf("/")+1);
  //     id="T"+id
  //
  //     this.load_style(name+"/css.css")
  //     // this.load_template(name+"/html.html",id)
  //     this.load_script(name+"/js.js")
  //   }
  // },
  load_script: function(url){
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = url;
    document.head.appendChild(script);
  },
  load_style: function(url){
    var link = document.createElement('link');
    link.type='text/css';
    link.rel = 'stylesheet';
    link.href = url;
    document.head.appendChild(link);
  },
  // load_template: function(url,id){
  //   // var script = document.createElement("script");
  //   // script.type = "text/x-template";
  //   // script.src = url;
  //   // script.id = id;
  //   // document.head.appendChild(script);
  //
  //   var script = document.createElement("template");
  //   // script.src = url;
  //   script.id = id;
  //   document.head.appendChild(script);
  //   $(script).load(url);
  // },

}

window.APP=APP;



})(jQuery);
